//
//  ZYHActivityIndicatorView.m
//  ZYHActivityIndicatorView
//
//  Created by mac on 15/9/29.
//  Copyright (c) 2015年 mac. All rights reserved.
//

#import "ZYHActivityIndicatorView.h"

@interface ZYHActivityIndicatorView ()

@end

@implementation ZYHActivityIndicatorView

static ZYHActivityIndicatorView *zyhActivityView = nil;


+ (ZYHActivityIndicatorView *)activityViewForView:(UIView *)addToView withLabelStr:(NSString *)labelText width:(NSUInteger)aLabelWidth andColor:(UIColor *)theColor{

    if (zyhActivityView) {
        
        [self removeView];
    }
    
    zyhActivityView = [[self alloc]initForView:addToView withLabel:labelText width:aLabelWidth andColor:theColor];
    return zyhActivityView;

}


- (ZYHActivityIndicatorView *)initForView:(UIView *)addToView withLabel:(NSString *)labelText width:(NSUInteger)aLabelWidth andColor:(UIColor *)theColor{
    
    if (!(self = [super initWithFrame:CGRectZero])) {
        return nil;
    }

    self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    
    //底部背景View
    self.shadeView = [[UIView alloc]initWithFrame:addToView.frame];
    self.shadeView.backgroundColor = [UIColor blackColor];
    self.shadeView.alpha = 0.1;

    //容器View
    self.borderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, aLabelWidth, 40)];
    self.borderView.center = addToView.center;
    self.borderView.backgroundColor = [UIColor clearColor];
    self.borderView.opaque = NO;
    self.borderView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
    
    self.activity = [[UIActivityIndicatorView alloc]init];
    self.activity.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
    self.activity.color = theColor;
    self.activity.frame = CGRectMake(aLabelWidth*0.5 - 10, 0, 20, 20);
    //NSLog(@"activityIndicator:%@",NSStringFromCGRect(self.activity.frame));
    [self.activity startAnimating];
    
    self.activityLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, aLabelWidth, 20)];
    self.activityLabel.font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
    self.activityLabel.textAlignment = NSTextAlignmentCenter;
    self.activityLabel.textColor = theColor;
    self.activityLabel.backgroundColor = [UIColor clearColor];
    self.activityLabel.text = labelText;
    
    [addToView addSubview:self.shadeView];
    [addToView addSubview:self];
    [self addSubview:self.borderView];
    [self.borderView addSubview:self.activity];
    [self.borderView addSubview:self.activityLabel];
    
    return self;
}








+ (ZYHActivityIndicatorView *)overActivityViewForView:(UIView *)addToView withLabelStr:(NSString *)labelText andColor:(UIColor *)theColor{

    if (zyhActivityView) {
        
        [self removeView];
    }
    
    zyhActivityView = [[self alloc]initForView:addToView withLabel:labelText andColor:theColor];

    return zyhActivityView;
}

- (ZYHActivityIndicatorView *)initForView:(UIView *)addToView withLabel:(NSString *)labelText andColor:(UIColor *)theColor{

    if (!(self = [super initWithFrame:CGRectZero])) {
        return nil;
    }
    
    //底部添加阴影View
    CGFloat screen_width = [UIScreen mainScreen].bounds.size.width;
    CGFloat screen_hight = [UIScreen mainScreen].bounds.size.height;
    CGRect aFrame = CGRectMake(0, 0, screen_width, screen_hight);
    self.shadeView = [[UIView alloc]initWithFrame:aFrame];
    self.shadeView.backgroundColor = [UIColor blackColor];
    self.shadeView.alpha = 0.1;
    self.coverWindow = [UIApplication sharedApplication].keyWindow;
    [self.coverWindow addSubview:self.shadeView];
    
    //添加一个等待进度轮
    self.activity = [[UIActivityIndicatorView alloc]init];
    self.activity.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
    self.activity.color = theColor;
    self.activity.center = addToView.center;
    [self.activity startAnimating];
    [self.coverWindow addSubview:self.activity];
    
    //添加一个Label显示状态
    aFrame = self.activity.frame;
    aFrame.origin.x = 0;
    aFrame.origin.y += 25;
    aFrame.size.width = screen_width;
    aFrame.size.height = 25;
    self.activityLabel = [[UILabel alloc]initWithFrame:aFrame];
    self.activityLabel.textColor = theColor;
    self.activityLabel.text = labelText;
    self.activityLabel.textAlignment = NSTextAlignmentCenter;
    [self.coverWindow addSubview:self.activityLabel];
    [addToView addSubview:self];
    
    return self;
}



+ (void)removeView{
    
    if (!zyhActivityView) return;

    [zyhActivityView stopActivity];
    
}

-(void)stopActivity{
    
    self.shadeView.hidden = YES;
    self.activityLabel.hidden = YES;
    [self.activity stopAnimating];
    [self removeFromSuperview];
    zyhActivityView = nil;
}



@end
